<?php
class Admin_AdminUserController extends Zendvn_Controller_Action{
	private $_paning = array('rowCount'=> 10, 'pageRange'=> 3);
	
	public function init(){
		parent::init();		
		$this->_templateObj->setPath(TEMPLATE_PATH . '/admin');		
		$this->_templateObj->load();	
		
		$pageNumber = $this->_request->getParam('page',1);
		$this->_paning['page'] 			= $pageNumber;
		$this->_paning['rowCount'] 		= $this->_configObj->getSite('list_limit');		
		$this->_paning['pageRange'] 	= 3;				
		$this->_arrParam['panigator'] 	= $this->_paning;
		$this->view->arrParam = $this->_arrParam;
	}
	
	public function indexAction(){
		$this->view->Title = $this->view->translate('Manager') . ' : '
								. ' : ' .  $this->view->translate('Users') . ' : '
							 	. ' : ' .  $this->view->translate('List');
		$this->view->headTitle($this->view->Title,true);
	}

	public function addAction(){
		$this->view->Title = $this->view->translate('Manager')  . ' : '
								. ' : ' .  $this->view->translate('Users') . ' : '
							 	. " : " . $this->view->translate('Add');
		$this->view->headTitle($this->view->Title,true);
		$this->view->headLink()->appendStylesheet(PUBLIC_URL . '/css/jquery-ui-1.9.2.css','screen');
		
		$tblGroup 					= new Zendvn_Model_Table_Group();
		$this->view->sblGroup 		= $tblGroup->itemInSelect();
		$this->view->sblExtGroup 	= $tblGroup->itemInSelect(null, 'ext-group');
		
		$tblUser = new Zendvn_Model_Table_User();
		
		if($this->_request->isPost()){
			$validator = new Admin_Form_ValidateUser($this->_arrParam);
			if($validator->isVaild()){
				$arrErrors = $validator->getMessageErrors();
				$response = array('type' => 'error', 'msg' => $arrErrors);
				echo json_encode($response);
			}else{
				$data =  $validator->getData();
				$tblUser->saveItem($data,array('task'=>'admin-add'));

				$response = array('type' => 'success', 'msg' => 'The update was successful');
				echo json_encode($response);
			}
			$this->_helper->viewRenderer->setNoRender();
			$this->_helper->layout->disableLayout();
		}
		
		$this->_helper->viewRenderer('form');
	}
	
	public function editAction(){
		$this->view->Title = $this->view->translate('Manager')  . ' : '
								. ' : ' .  $this->view->translate('Users') . ' : '
							 	. " : " . $this->view->translate('Edit');
		$this->view->headTitle($this->view->Title,true);
		
		$tblUser = new Zendvn_Model_Table_User();
		$this->view->Item = $tblUser->getItem($this->_arrParam, array('task' => 'info'));
		
		$tblGroup = new Zendvn_Model_Table_Group();
		$this->view->sblGroup = $tblGroup->itemInSelect();
		
		if($this->_request->isPost()){
			$validator = new Admin_Form_ValidateUser($this->_arrParam);
			if($validator->isVaild()){
				$arrErrors = $validator->getMessageErrors();
				$response = array('type' => 'error', 'msg' => $arrErrors);
				echo json_encode($response);
			}else{
				$data =  $validator->getData();		
				
				$tblUser = new Zendvn_Model_Table_User();
				$tblUser->saveItem($data,array('task'=>'admin-edit'));
				$response = array('type' => 'success', 'msg' => 'The update was successful');
				echo json_encode($response);
			}
			$this->_helper->viewRenderer->setNoRender();
			$this->_helper->layout->disableLayout();
		}
		$this->_helper->viewRenderer('form');	
	}
	
	public function infoAction(){
		$this->view->Title = $this->view->translate('Manager') . ' : '
							. ' : ' .  $this->view->translate('Users') . ' : '
							. " : " . $this->view->translate('Info');
		$this->view->headTitle($this->view->Title,true);
		
		$tblUser = new Zendvn_Model_Table_User();
		$this->view->Item = $tblUser->getItem($this->_arrParam,array('task'=>'info'));
	}
	
	public function deleteAction(){
		$this->view->Title = "Member :: Member manager :: Delete";
		$this->view->headTitle($this->view->Title,true);		
		$tblUser = new Zendvn_Model_Table_User();
		
		if($this->_arrParam['type'] == 'multi'){
			$tblUser->deleteItem($this->_arrParam, array('task'=>'multi'));
		}else{
			$tblUser->deleteItem($this->_arrParam);
		}
		$this->_helper->flashMessenger->addMessage(array('success'=>'The delete was successful'));
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function statusAction(){
		$tblUser = new Zendvn_Model_Table_User();
	
		if(@$this->_arrParam['type'] == 'multi'){
			$tblUser->statusItem($this->_arrParam,array('task'=>'multi'));
		}else{
			$tblUser->statusItem($this->_arrParam);
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
	}
	
	public function loadDataTableAction(){
		$arrStatusLabel = array('active' => 'Active', 'inactive' => 'Inactive');
		$tblItem 	= new Zendvn_Model_Table_User();
		$result 	= $tblItem->listItem($this->_arrParam, array('task'=>'list-dbtable'));

		$items 		= $result['data'];
		foreach($items as $k => $v){
			$linkInfo = $this->view->cmsUrl(array(
												'action' => 'info',
												'id' => $v['id']
												));
			$linkEdit = $this->view->cmsUrl(array(
												'action' => 'edit',
												'id' => $v['id']
												));
			
			$items[$k]['ckb'] = '<label class="position-relative">
									<input name="cid[]" id="cid" value="' . $v['id'] . '" type="checkbox" class="ace">
									<span class="lbl"></span>
									</label>';
			$lblStatus			= $arrStatusLabel[$items[$k]['status']];
			if($items[$k]['status'] == 'active'){
				$clssStatus = 'label label-success arrowed-in';
			}else{
				$clssStatus = 'label arrowed-in';
			}
			$items[$k]['status'] = '<span id="tbl-status-btn" data-status="' . $items[$k]['status'] . '" class="' . $clssStatus . '">' . $lblStatus . '</span>'; 						
									
			$items[$k]['control'] = '<div class="btn-group">
											<a href="' . $linkInfo . '" class="btn btn-mini btn-success">
												<i class="icon-zoom-in bigger-120"></i>
											</a>
											<a href="' . $linkEdit . '" class="btn btn-mini btn-info">
												<i class="icon-edit bigger-120"></i>
											</a>
											<button id="tbl-delete-btn" class="btn btn-mini btn-danger">
												<i class="icon-trash bigger-120"></i>
											</button>
										</div>';
		}
		$output = array(
						"sEcho" => intval(@$this->_arrParam['sEcho']),
						"iTotalRecords" => $result['total'],
						"iTotalDisplayRecords" => $result['total'],
						"aaData" => $items
					);
		echo json_encode($output);
		
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
	}
}